import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

GroupBox {
    title: "用户信息管理"

    ColumnLayout {
        width: parent.width
        spacing: 15

        Label {
            text: "用户信息管理"
            font.bold: true
            font.pixelSize: 20
            Layout.alignment: Qt.AlignHCenter
        }

        // 姓名输入
        Label { text: "姓名:" }
        TextField {
            id: nameField
            Layout.fillWidth: true
            placeholderText: "请输入姓名"
            text: userManager.name
            onTextChanged: userManager.name = text
        }

        // 等级输入
        Label { text: "等级:" }
        SpinBox {
            id: levelField
            Layout.fillWidth: true
            from: 0
            to: 100
            value: userManager.level
            onValueChanged: userManager.level = value
        }

        // 可用积分输入
        Label { text: "可用积分:" }
        SpinBox {
            id: availablePointsField
            Layout.fillWidth: true
            from: 0
            to: 100000
            value: userManager.availablePoints
            onValueChanged: userManager.availablePoints = value
        }

        // 今日积分输入
        Label { text: "今日积分:" }
        SpinBox {
            id: todayPointsField
            Layout.fillWidth: true
            from: 0
            to: 10000
            value: userManager.todayPoints
            onValueChanged: userManager.todayPoints = value
        }

        // 总积分输入
        Label { text: "总积分:" }
        SpinBox {
            id: totalPointsField
            Layout.fillWidth: true
            from: 0
            to: 1000000
            value: userManager.totalPoints
            onValueChanged: userManager.totalPoints = value
        }

        // 按钮区域
        RowLayout {
            Layout.fillWidth: true
            spacing: 10

            Button {
                text: "保存到JSON"
                Layout.fillWidth: true
                onClicked: {
                    userManager.saveUserData()
                    saveMessage.text = "用户数据保存成功！"
                    saveMessage.visible = true
                    saveMessageTimer.restart()
                }
            }

            Button {
                text: "重新加载"
                Layout.fillWidth: true
                onClicked: {
                    userManager.loadUserData()
                    reloadMessage.text = "用户数据重新加载成功！"
                    reloadMessage.visible = true
                    reloadMessageTimer.restart()
                }
            }

            Button {
                text: "重置为默认值"
                Layout.fillWidth: true
                onClicked: {
                    userManager.name = ""
                    userManager.level = 0
                    userManager.availablePoints = 0
                    userManager.todayPoints = 0
                    userManager.totalPoints = 0
                }
            }
        }

        // 当前值显示区域
        GroupBox {
            title: "当前值"
            Layout.fillWidth: true

            ColumnLayout {
                width: parent.width
                spacing: 5

                Label { text: "姓名: " + userManager.name }
                Label { text: "等级: " + userManager.level }
                Label { text: "可用积分: " + userManager.availablePoints }
                Label { text: "今日积分: " + userManager.todayPoints }
                Label { text: "总积分: " + userManager.totalPoints }
            }
        }

        // 保存成功消息
        Label {
            id: saveMessage
            Layout.fillWidth: true
            color: "green"
            visible: false
            horizontalAlignment: Text.AlignHCenter
        }

        // 重新加载成功消息
        Label {
            id: reloadMessage
            Layout.fillWidth: true
            color: "blue"
            visible: false
            horizontalAlignment: Text.AlignHCenter
        }
    }

    Timer {
        id: saveMessageTimer
        interval: 3000
        onTriggered: saveMessage.visible = false
    }

    Timer {
        id: reloadMessageTimer
        interval: 3000
        onTriggered: reloadMessage.visible = false
    }
}
